package com.kennedy.springbootjpa.service;

import com.kennedy.springbootjpa.beans.Shop;
import com.kennedy.springbootjpa.repostory.ShopRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tao.zeng on 2017/7/28.
 */
@Service
public class ShopService {
    @Autowired
    private ShopRepository shopRepository;

    public void insert(Shop shop) {
        shopRepository.save(shop);
    }

    public Shop selectOne(long id) {
        return null;
    }

    public List<Shop> selectAll() {
        return null;
    }

    public Page<Shop> getListByPage(Shop shop) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withIgnorePaths("pageNumber", "pageSize");
        Example<Shop> shopExample = Example.of(shop, matcher);
        Page<Shop> shops = shopRepository.findAll(shopExample, new PageRequest(shop.getPageNumber() - 1, shop.getPageSize()));
        return shops;
    }
}
